//
//  PlaceHolderTextView.m
//  微博2期
//
//  Created by J on 15/12/7.
//
//

#import "PlaceHolderTextView.h"

@implementation PlaceHolderTextView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        
//        self.delegate = self;//不要设置自己的delegate为自己  因为有个delegate属性可以把代理覆盖了
    
        //通知
        //当UITextView的文字发生改变时 自己会发出一个 UITextViewTextDidChangeNotification通知
        [NoficationCenter addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];//监听自己发出的通知
    
    
    }
    return self;
    
}


/**
 *  监听文字改变
 */
- (void)textDidChange{
    [self setNeedsDisplay];//重绘 会自动重新调用 drawRect 方法不要手动调用
    //每当调用一次drawRect  就会把之前的东西擦掉
}



- (void)drawRect:(CGRect)rect{
    
//    NSLog(@"drawRect");
//    [RamdomColor set];
//    UIRectFill(CGRectMake(20, 20, 20, 20));
    
    if (self.hasText) return;//如果有文字就return 为了不画占位文字
    //每当调用一次drawRect  就会把之前的东西擦掉
        //文字属性
        NSMutableDictionary * atts = [NSMutableDictionary dictionary];
        atts[NSFontAttributeName] = self.font;
        atts[NSForegroundColorAttributeName] = self.placeholderColor;
        //画文字
//        [self.placeHolder drawAtPoint:CGPointMake(5, 8) withAttributes:atts];
    CGFloat x = 5;
    CGFloat w = rect.size.width-2*x;
    CGFloat y = 8;
    CGFloat h =rect.size.height-2*y;
    [self.placeHolder drawInRect:CGRectMake(x, y, w, h) withAttributes:atts];

}

@end
